//
//  WorldCupRun.swift
//  swiftStudy
//
//  Created by unisedu on 16/4/1.
//  Copyright © 2016年 CaoFei. All rights reserved.
//

import UIKit

class WorldCupRun: NSObject {

}
//小球动画
//gotoxy(5,1)
//println("hello world")
//gotoxy(10,15)
//println("hello world")
//gotoxy(15,1)
//println("hello world")
//for  i in 1..50
//{
//    gotoxy(10,(CInt)(i))
//    println("o")
//    //老位置擦除
//    gotoxy(10,(CInt)(i-1))
//    println(" ")
//    usleep(100000)
//}

var allTeamsName = ["比利时","波黑","克罗地亚","英格兰","法国","德国","希腊","意大利","荷兰","葡萄牙","俄罗斯","瑞士","西班牙","阿根廷","巴西","智利","哥伦比亚","厄瓜多尔","乌拉圭","哥斯达黎加","洪都拉斯","墨西哥","美国","阿尔及利亚","喀麦隆","加纳","科特迪瓦","尼日利亚","澳大利亚","伊朗","日本","韩国"]
var allTeamsOdds : [Double]=[2.6,800,100,125,12,40,800,7.5,28,75,300,600,75,24,400,13,100,200,700,19,4.4,300,700,200,4.1,225,175,18,25,700,300,110]

//struct Point {
//    var row = 0
//    var col = 0
//}
//
//struct Size {
//    var width = 0
//    var height = 0
//}
//
//struct Rect{
//    var origin = Point()
//    var size  = Size()
//}

class Team{
    var name: String
    var odds: Double
    var groupName: String = " "
    var contestNum = 0
    var no = 0
    var frame = Rect()
    
    init(name:String , odds:Double ){
        self.name = name
        self.odds = odds
    }
    
    func drawRect(){
        gotoxy((CInt)(frame.origin.row),(CInt)(frame.origin.col))
        print(name)
        gotoxy((CInt)(frame.origin.row+1),(CInt)(frame.origin.col))
        print(odds)
        gotoxy((CInt)(frame.origin.row+2),(CInt)(frame.origin.col))
        print(contestNum)
        gotoxy((CInt)(frame.origin.row+3),(CInt)(frame.origin.col))
        print(no)
    }
}

var allTeams = [Team]()
func creatAllTeams(){
    for i in 0...32{
        let name = allTeamsName[i]
        let odds = allTeamsOdds[i]
        var team = Team(name:name,odds:odds)
        team.no = i+1
        allTeams += team
    }
}

func setGroup(){
    
    for i in 0...32 {
        if i < 4 {
            allTeams[i].groupName = "A组"
        }else if i < 8 {
            allTeams[i].groupName = "B组"
        }else if i < 12 {
            allTeams[i].groupName = "C组"
        }else if i < 16 {
            allTeams[i].groupName = "D组"
        }else if i < 20 {
            allTeams[i].groupName = "E组"
        }else if i < 24 {
            allTeams[i].groupName = "F组"
        }else if i < 28 {
            allTeams[i].groupName = "G组"
        }else{
            allTeams[i].groupName = "H组"
        }
        
    }
}


func setFrame () {
    var rect = Rect()
    rect.size.width = 10
    rect.size.height = 3
    for i in 0...32 {
        let a = i/4
        let b = i%4
        rect.origin.row = 3 + 5 * a
        rect.origin.col = 12 + 9 * b
        allTeams[i].frame = rect
        allTeams[i].drawRect()
    }
}

func printTable() {
    gotoxy(1,1)
    print("           2014世界杯精彩程序")
    gotoxy(2, 1)
    print("类别")
    gotoxy(2,12)
    print("类别")
    gotoxy(2,21)
    print("第二档")
    gotoxy(2,30)
    print("第三档")
    gotoxy(2,39)
    print("第四档")
    
    for var i = 0 ;i<40;i+=5{
        gotoxy ((CInt)(i+3),1)
        switch i {
        case 0:print("A组")
        case 5: print("B组")
        case 10: print("C组")
        case 15:print("D组")
        case 20:print("E组")
        case 25:print("F组")
        case 30:print("G组")
        case 35:print("H组")
        default:
            break
        }
        gotoxy ((CInt)(i+4),1)
        print("夺冠赔率")
        gotoxy ((CInt)(i+5),1)
        print("投注人数")
        gotoxy ((CInt)(i+6),1)
        print("编号")
    }
}

func getSelectedTeams(number : Int ) -> [Int] {
    var selectedTeams = [Int](count:number , repeatedValue:0)
    for i in 0...number {
        selectedTeams[i] = (Int)(getInt())
        
    }
    return selectedTeams
}

func contestSelect () {
    gotoxy (43,1)
    print("   1.竞猜16强 2.竞猜8强 3.竞猜4强 4.竞猜冠军")
    let choose = (Int)(getInt())
    var selectedTeams = [Int]()
    switch choose {
    case 1:
        print("请输入16强球队编号：")
        selectedTeams = getSelectedTeams(16)
    case 2:
        print("请输入8强球队编号：")
        selectedTeams = getSelectedTeams(8)
    case 3:
        print("请输入4强球队编号：")
        selectedTeams = getSelectedTeams(4)
    case 4:
        print("请输入冠军球队编号：")
        selectedTeams = getSelectedTeams(1)
    default:
        break
    }
    
    //修改球队信息
    for num in selectedTeams {
        allTeams[num - 1].contestNum++
        allTeams[num - 1].drawRect()
    }
    //擦除老的输入信息
    sleep(1)
    gotoxy(44, 1)
    print("                   ")
    gotoxy(45, 1)
    print("                   ")
    gotoxy(46, 1)
    print("                   ")
}

//
sleep(5)
system("clear screen")
creatAllTeams()
setGroup()
setFrame()
printTable()
while true{
    contestSelect()
}

//允许用户输入竞猜